/**
 * Created with IntelliJ IDEA.
 * Description:搜索旋转排序数组中的最小值
 * User: bjc
 * Date: 2025-11-17
 * Time: 14:03
 */
public class Test07_findMin {
    public int findMin(int[] nums) {
        int left = 0, right = nums.length - 1;
        int tmp = nums[nums.length - 1];
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > tmp)
                left = mid + 1;
            else
                right = mid;
        }
        return nums[left];
    }
    //选择数组左端点作为参照点
    public int findMin1(int[] nums) {
        int left = 0, right = nums.length - 1;
        int tmp = nums[0];
        // 如果数组未旋转，直接返回第一个元素
        if (nums[right] >= nums[0]) {
            return nums[0];
        }
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] >= tmp)
                left = mid + 1;
            else
                right = mid;
        }
        return nums[left];
    }
}
